<?php require_once('common/mysql.php');?>
<?php  
// 创建Sql类的实例  
$db = new Sql();  
  
// 处理注册请求  
if ($_SERVER['REQUEST_METHOD'] == 'POST') {  
    $username = $_POST['username'];  
    $password = $_POST['pwd'];  
  
    // 检查用户名是否已存在  
    $checkUserExists = $db->get_row("SELECT * FROM user WHERE username = '$username'");

    if ($checkUserExists) {  
        // 用户名已存在，注册失败  
        echo "<script>alert(\"注册失败：用户名已存在。\");window.location.href = ''</script>";  
    } else {  
        // 用户名不存在，插入新用户  
        $sql = "INSERT INTO user (username, password, budget) VALUES ('$username', '$password',0)";
        if ($db->query($sql)) {  
            echo "<script>alert(\"注册成功！\");window.location.href = 'login.php'</script>";  
        } else {  
            echo "注册失败：数据库错误。";  
        }  
    }  
}  
?>  
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>Hadi.记账注册</title>
		<link rel="stylesheet" href="css/login.css" />
		<link rel="stylesheet" href="css/bootstrap.min.css" />
	</head>
	<body>
		<div class="container d-flex justify-content-center align-items-center loginDiv">
			<div class="row justify-content-center loginform">
				<div class="col">
					<div class="row justify-content-center loginTitle">
						Hadi.记账注册
					</div>
					<form action="" method="post">
						<div class="row inputDiv justify-content-center">
							<label class="labcss">用户名：</label>
							<input type="text" placeholder="输入你的用户名" name="username" class="username" />
						</div>
						<div class="row inputDiv justify-content-center">
							<label class="labcss">密码：</label>
							<input type="password" placeholder="输入你的密码" name="pwd" class="pwd" />
						</div>
						<div class="row inputDiv justify-content-center rowBtn">
							<input type="submit" value="注册" class="loginbtn"/>
						</div>
					</form>
				</div>
			</div>
		</div>
	</body>
</html>